package leju.common.widget;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.TextView;

public class AutoAlphaTextView extends TextView {

	private AlphaAnimation showAnimation;
	private AlphaAnimation hideAnimation;
	
	public AutoAlphaTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		init();
	}

	public AutoAlphaTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		init();
	}

	public AutoAlphaTextView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init();
	}

	private void init(){
		showAnimation = new AlphaAnimation(0.0f, 1.0f);
		showAnimation.setFillAfter(true);
		showAnimation.setDuration(1000);
		
		hideAnimation = new AlphaAnimation(1.0f, 0.0f);
		hideAnimation.setFillAfter(true);
		hideAnimation.setDuration(1000);
	}
	
	public void show(){
		setVisibility(View.VISIBLE);
		this.startAnimation(showAnimation);
		mHandler.sendEmptyMessageDelayed(0, 3000);
	}
	
	public void hide(){
		//setVisibility(View.GONE);
		this.startAnimation(hideAnimation);
		hideAnimation.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				setVisibility(View.GONE);
			}
		});
	}
	
	private Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			hide();
		}
		
	};
}
